package gui;

import java.util.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;

public class CheckBoxList{
	
	protected String[] strs;
	protected ArrayList<String> input = new ArrayList<String>();
	protected JScrollPane sp;
	
	public CheckBoxList(ArrayList<String> input){
		
		this.input = input;
		
		strs = new String[input.size()];
		strs = input.toArray(strs);
	
		final JList list = new JList(createData(strs));
		
		list.setCellRenderer(new CheckListRenderer());
		
	
		//Settare la lista.
	    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    
	    list.setBorder(new EmptyBorder(0, 4, 0, 0));
	    
	    list.addMouseListener(new MouseAdapter() {
	        public void mouseClicked(MouseEvent e) {
	          int index = list.locationToIndex(e.getPoint());
	          CheckableItem item = (CheckableItem) list.getModel().getElementAt(index);
	          item.setSelected(!item.isSelected());
	          Rectangle rect = list.getCellBounds(index, index);
	          list.repaint(rect);
	        }
	      });
	    
	    sp = new JScrollPane(list);
			
	}
	
	 private CheckableItem[] createData(String[] strs) {
		    int n = strs.length;
		    CheckableItem[] items = new CheckableItem[n];
		    for (int i = 0; i < n; i++) {
		      items[i] = new CheckableItem(strs[i]);
		      //just a test
		      /*if(items[i].toString().contains("(deleted)")){
					items[i].setSelected(false);
					
				}*/
		    }
		    return items;
	}
	 
	 // test select all deleted files
	 private CheckableItem[] selectDeletedFiles(String[] strs) {
		    int n = strs.length;
		    CheckableItem[] items = new CheckableItem[n];
		    for (int i = 0; i < n; i++) {
		      items[i] = new CheckableItem(strs[i]);
		      //just a test
		      if(items[i].toString().contains("(deleted)") && (items[i].isSelected == false)){
					items[i].setSelected(true);
					
				}
		    }
		    return items;
	}
	 
	 // test deselect all deleted files
	 private CheckableItem[] deselectDeletedFiles(String[] strs) {
		    int n = strs.length;
		    CheckableItem[] items = new CheckableItem[n];
		    for (int i = 0; i < n; i++) {
		      items[i] = new CheckableItem(strs[i]);
		      //just a test
		      if(items[i].toString().contains("(deleted)")){
					items[i].setSelected(false);	
				}
		    }
		    return items;
	}
	
	class CheckableItem {
	    private String str;

	    private boolean isSelected;

	    public CheckableItem(String str) {
	      this.str = str;
	      isSelected = true;
	    }

	    public void setSelected(boolean b) {
	      isSelected = b;
	    }

	    public boolean isSelected() {
	      return isSelected;
	    }

	    public String toString() {
	      return str;
	    }
	  }
	
	class CheckListRenderer extends JCheckBox implements ListCellRenderer {

	    public CheckListRenderer() {
	      setBackground(UIManager.getColor("List.textBackground"));
	      setForeground(UIManager.getColor("List.textForeground"));
	    }

	    public Component getListCellRendererComponent(JList list, Object value,
	        int index, boolean isSelected, boolean hasFocus) {
	      setEnabled(list.isEnabled());
	      setSelected(((CheckableItem) value).isSelected());
	      setFont(list.getFont());
	      setText(value.toString());
	      return this;
	    }
	  }


}
